'use strict';

angular.module('myTimeLine')
	.factory("Main",['$http','$localStorage',function($http,$localStorage){
        var baseUrl='http://101.200.162.145:88/time2time/duanyongpeng/dyp_T/api/web/v1/';
		// var baseUrl='http://localhost/dyp_T/api/web/v1/';
		
		function changeUser(user){
			angular.extend(currentUser,user);
		}

		function urlBase64Decode(str) {
            var output = str.replace('-', '+').replace('_', '/');
            switch (output.length % 4) {
                case 0:  
                    break;
                case 2:
                    output += '=='; 
                    break; 
                case 3:
                    output += '=';
                    break;
                default:
                    throw 'Illegal base64url string!';
            }
            return window.atob(output);
        }

        function getUserFromToken(){
        	var token=$localStorage.token;
        	var user ={};
        	if(typeof token!=='undefined'){
        		var encoded=token.split('.')[1];
        		user=JSON.parse(urlBase64Decode(encoded));
        	}
        	return user;
        }
        var currentUser=getUserFromToken();


        return {
        	save:function(data,success,error){
        		//todos
        	},
        	signin:function(data, success,error){
        		$http.get(baseUrl+'countries?sdfdsf=sss',data,{'Content-Type':'application/json'}).success(success).error(error);
        	}
        }
	}	
])
    .factory('TopicService',['$http',function($http){
        var baseUrl='http://api.dev.time2time.cn/v1/';
        return{
            getAllTopics:function(success,error){
                $http.get(baseUrl+'topics')
                    .success(success).error(error);
            }
        }
    }]);











